package com.sjgtw.cloud.web.reservoir.service.impl;


import com.sjgtw.cloud.common.core.constant.CacheConstants;
import com.sjgtw.cloud.common.core.utils.DateUtils;
import com.sjgtw.cloud.common.core.utils.SecurityUtils;
import com.sjgtw.cloud.common.core.utils.StringUtils;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.redis.service.RedisService;
import com.sjgtw.cloud.service.SystemServerService;
import com.sjgtw.cloud.system.api.model.LoginUser;
import com.sjgtw.cloud.web.reservoir.domain.Reservoir;
import com.sjgtw.cloud.web.reservoir.mapper.ReservoirMapper;
import com.sjgtw.cloud.web.reservoir.service.IReservoirService;
import com.sjgtw.cloud.web.waterDamage.domain.WaterDamage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.regex.Pattern;

/**
 * 水库分布Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-11
 */
@Service
public class ReservoirServiceImpl implements IReservoirService
{
    @Autowired
    private ReservoirMapper reservoirMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private SystemServerService systemServerService;


    /**
     * 查询水库分布
     * 
     * @param id 水库分布主键
     * @return 水库分布
     */
    @Override
    public Reservoir selectReservoirById(Long id)
    {
        return reservoirMapper.selectReservoirById(id);
    }


    public static boolean isDouble(String str) {
        try {
            double value = Double.parseDouble(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * 查询水库分布列表
     * 
     * @param reservoir 水库分布
     * @return 水库分布
     */
    @Override
    public List<Reservoir> selectReservoirList(Reservoir reservoir)
    {
        List<Reservoir> reservoirList = reservoirMapper.selectReservoirList(reservoir);
        if(reservoirList.size()>0){
            for (int i = 0; i < reservoirList.size(); i++) {
                StringBuilder sb = new StringBuilder();
                String kilometer = reservoirList.get(i).getKilometer();
                if(isDouble(kilometer)){
                    String[] split = kilometer.split("\\.");
                    if(split.length==2){
                        if(StringUtils.isNotEmpty(split[0])){
                            sb.append("K");
                            sb.append(split[0]);
                        }
                        if(StringUtils.isNotEmpty(split[1])){
                            sb.append("+");
                            sb.append(split[1]);
                        }
                    }else if(split.length ==1){
                        if(StringUtils.isNotEmpty(split[0])){
                            sb.append("K");
                            sb.append(split[0]);
                            sb.append("+0");
                        }
                    }
                }

                reservoirList.get(i).setKilometer(sb.toString());

            }
        }

        return reservoirList;
    }

    /**
     * 新增水库分布
     * 
     * @param reservoir 水库分布
     * @return 结果
     */
    @Override
    public int insertReservoir(Reservoir reservoir)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);

        reservoir.setCreateBy(user.getUsername());
        reservoir.setCreateTime(new Date());
        return reservoirMapper.insertReservoir(reservoir);
    }

    /**
     * 修改水库分布
     * 
     * @param reservoir 水库分布
     * @return 结果
     */
    @Override
    public int updateReservoir(Reservoir reservoir)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);

        reservoir.setUpdateBy(user.getUsername());
        reservoir.setUpdateTime(new Date());
        return reservoirMapper.updateReservoir(reservoir);
    }

    /**
     * 批量删除水库分布
     * 
     * @param ids 需要删除的水库分布主键
     * @return 结果
     */
    @Override
    public int deleteReservoirByIds(Long[] ids)
    {
        return reservoirMapper.deleteReservoirByIds(ids);
    }

    /**
     * 删除水库分布信息
     * 
     * @param id 水库分布主键
     * @return 结果
     */
    @Override
    public int deleteReservoirById(Long id)
    {
        return reservoirMapper.deleteReservoirById(id);
    }

    @Override
    public List<String> check(List<Reservoir> entities) {
        List<String> errors = new ArrayList<>();
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);

        for (Reservoir entity : entities) {
            if (user != null) {
                entity.setCreateBy(user.getUsername());
            }
            entity.setCreateTime(DateUtils.getNowDate());
            String kilometer = entity.getKilometer();
            if(StringUtils.isNotEmpty(kilometer)){
                if(kilometer.startsWith("K")){
                    if(kilometer.contains("+")) {
                        int addIndex = kilometer.indexOf("+");

                        kilometer = kilometer.substring(1,addIndex)+"."+kilometer.substring(addIndex+1,kilometer.length());

                    }else{
                        kilometer = kilometer.substring(1,kilometer.length());
                    }

                }
            }
            entity.setKilometer(kilometer);
        }
        return errors;
    }


    @Override
    public void batchSave(List<Reservoir> entities) {
        if (entities != null && entities.size() > 0) {
            if (entities.size() >= 200) {
                int index = 0;
                while (index <= entities.size()) {
                    int last = Math.min(index + 200, entities.size());
                    List<Reservoir> temp = entities.subList(index, last );
                    if (temp != null && temp.size() > 0) {
                        reservoirMapper.batchSave(temp);
                    }
                    index += 200;
                }
            } else {
                reservoirMapper.batchSave(entities);
            }
        }
    }



}
